package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.constants.ControlCharacter;
import com.xy.entity.*;
import com.xy.mapper.ExhibitionMapper;
import com.xy.request.HeartbeatRequest;
import com.xy.response.*;
import com.xy.service.*;
import com.xy.utils.StrUtil;
import com.xy.vo.InitVO;
import com.xy.wrapper.InitWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@AllArgsConstructor
@Service
public class IInspectBaseServiceImpl implements IInspectBaseService {

	private final IInitService initService;
	private final IGateService gateService;
	private final IPositionGateService positionGateService;
	private final IPositionService positionService;
	private final IVenueService venueService;
	private final IPositionGroupRelService positionGroupRelService;
	private final IPositionGroupService positionGroupService;
	private final IExhibitionPositionService exhibitionPositionService;
	private final ExhibitionMapper exhibitionMapper;
	private final IRuleService ruleService;
	private final IVersionService versionService;

	@Override
	public GateEntity getGateEntity(String gateRef) {
		return gateService.getOne(Wrappers.<GateEntity>query().lambda().eq(GateEntity::getGateRef, gateRef));
	}

	@Override
	public List<ExhibitionEntity> getListByPiontId(String positionId) {
		List<ExhibitionPositionEntity> exhibitionProintList = exhibitionPositionService.list(Wrappers.<ExhibitionPositionEntity>query().lambda().eq(ExhibitionPositionEntity::getPositionId, positionId));
		if (CollectionUtil.isNotEmpty(exhibitionProintList)) {
			List<Long> longExList = exhibitionProintList.stream().map(ExhibitionPositionEntity::getExhibitionId).collect(Collectors.toList());
			LambdaQueryWrapper<ExhibitionEntity> exhibitionEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
			exhibitionEntityLambdaQueryWrapper.eq(ExhibitionEntity::getCloseStatus, ControlCharacter.CLOSE_STATUS_YES);
			exhibitionEntityLambdaQueryWrapper.in(ExhibitionEntity::getId, longExList);
			List<ExhibitionEntity> exhibitionList = exhibitionMapper.selectList(Wrappers.<ExhibitionEntity>query().lambda().
				eq(ExhibitionEntity::getCloseStatus, ControlCharacter.CLOSE_STATUS_YES)
				.in(ExhibitionEntity::getId, longExList)
			);
			return exhibitionList;
		}
		return null;
	}

	@Override
	public InitVO getInit(String venueCode) {
		InitEntity initEntity = initService
			.getOne(Wrappers.<InitEntity>query().lambda().eq(InitEntity::getVenueCode,
				venueCode));
		if (initEntity == null) {
			throw new ServiceException("code未找到相关初始化信息!");
		}
		return InitWrapper.build().entityVO(initEntity);
	}

	@Override
	public GateResponse gataSave(String gateRef) {
		GateEntity gateEntity = getGateEntity(gateRef);
		GateResponse gateResponse = BeanUtil.newInstance(GateResponse.class);
		if (Func.isEmpty(gateEntity)) {
			gateEntity = BeanUtil.newInstance(GateEntity.class);
			gateEntity.setGateRef(gateRef);
			gateEntity.setGateName(StrUtil.deviceNo());
			gateService.save(gateEntity);
			gateResponse.setGateId(gateEntity.getId());
			gateResponse.setGateName(gateEntity.getGateName());
			return gateResponse;
		}
		PositionGateEntity positionGate = positionGateService.getOne(Wrappers.<PositionGateEntity>query().lambda().eq(PositionGateEntity::getGateId, gateEntity.getId()));
		if (Func.isEmpty(positionGate)) {
			return gateResponse;
		}
		PositionEntity positionEntity = positionService.getById(positionGate.getPositionId());
		return getGateResponse(gateEntity, positionEntity, gateResponse);
	}

	private GateResponse data(GateResponse gateResponse, GateEntity gateEntity, PositionEntity positionEntity, VenueEntity venueEntity, PositionGroupEntity positionGroupEntity) {
		gateResponse.setGateId(gateEntity.getId());
		gateResponse.setGateName(gateEntity.getGateName());
		gateResponse.setGateRef(gateEntity.getGateRef());
		gateResponse.setPositionName(positionEntity.getPositionName());
		gateResponse.setPositionId(positionEntity.getId());
		gateResponse.setVenueId(venueEntity.getId());
		gateResponse.setVenueName(venueEntity.getVenueName());
		gateResponse.setGroupName(positionGroupEntity.getGroupName());
		gateResponse.setPositionGroupId(positionGroupEntity.getId());
		return gateResponse;
	}

	@Override
	public GateResponse positionGateRel(String gateRef, String positionCode) {
		GateEntity gateEntity = getGateEntity(gateRef);
		if (gateEntity == null) {
			throw new ServiceException("该闸机未注册、请重启设备进行注册！");
		}
		PositionEntity positionEntity = positionService.getOne(Wrappers.<PositionEntity>query().lambda().eq(PositionEntity::getPositionCode, positionCode));
		if (positionEntity == null) {
			throw new ServiceException("该门禁点不存在、请联系管理员进行创建！");
		}
		PositionGateEntity positionGate = positionGateService.getOne(Wrappers.<PositionGateEntity>query().lambda()
			.eq(PositionGateEntity::getGateId, gateEntity.getId())
			.or()
			.eq(PositionGateEntity::getPositionId, positionEntity.getId())
		);
		if (Func.isEmpty(positionGate)) {
			positionGate = BeanUtil.newInstance(PositionGateEntity.class);
		}
		positionGate.setPositionId(positionEntity.getId());
		positionGate.setGateId(gateEntity.getId());
		positionGate.setPositionName(positionEntity.getPositionName());
		positionGateService.saveOrUpdate(positionGate);
		GateResponse gateResponse = BeanUtil.newInstance(GateResponse.class);
		return getGateResponse(gateEntity, positionEntity, gateResponse);

	}

	@Override
	public HeartbeatResponse getHeartbeat(HeartbeatRequest heartbeatRequest) {
		GateEntity gateEntity = getGateEntity(heartbeatRequest.getGateRef());
		if (gateEntity == null) {
			throw new ServiceException("该闸机未注册、请重启设备进行注册！");
		}
		PositionEntity positionEntity = positionService.getById(heartbeatRequest.getPositionId());
		if (positionEntity == null) {
			throw new ServiceException("该门禁点不存在、请联系管理员进行创建！");
		}
		PositionGroupRelEntity positionGroupRel = positionGroupRelService.getOne(Wrappers.<PositionGroupRelEntity>query().lambda().eq(PositionGroupRelEntity::getPositionId, positionEntity.getId()));
		//基本信息
		PositionGroupEntity positionGroupEntity = positionGroupService.getById(positionGroupRel.getPositionGroupId());
		VenueEntity venueEntity = venueService.getById(positionEntity.getVenueId());
		HeartbeatResponse heartbeatResponse = BeanUtil.newInstance(HeartbeatResponse.class);
		heartbeatResponse.setGateName(gateEntity.getGateName());
		heartbeatResponse.setGateRef(gateEntity.getGateRef());
		heartbeatResponse.setGateId(gateEntity.getId());
		heartbeatResponse.setPositionName(positionEntity.getPositionName());
		heartbeatResponse.setPositionId(Func.toStr(positionEntity.getId()));
		heartbeatResponse.setGroupName(positionGroupEntity.getGroupName());
		heartbeatResponse.setPositionGroupId(positionGroupEntity.getId());
		heartbeatResponse.setVenueName(venueEntity.getVenueAs());
		heartbeatResponse.setVenueId(venueEntity.getId());
		//查询展会
		List<ExhibitionEntity> exhibitionList = getListByPiontId(heartbeatRequest.getPositionId());
		if (Func.isNotEmpty(exhibitionList)) {

			List<ExhibitionResponse> exhibitionResponseList = new ArrayList<>();
			exhibitionList.forEach(o->{
				ExhibitionResponse exhibitionResponse = BeanUtil.newInstance(ExhibitionResponse.class);
				exhibitionResponse.setExhibitionId(o.getId());
				exhibitionResponse.setExhibitionName(o.getExhibitionAs());
				exhibitionResponseList.add(exhibitionResponse);
			});
//			List<Long> longList = exhibitionList.stream().map(ExhibitionEntity::getId).collect(Collectors.toList());
			heartbeatResponse.setExhibitionResponses(exhibitionResponseList);
			RuleEntity rule = null;
			//判断策略获取方式
			if (ControlCharacter.ADMISSION_RULE_ID.equals(positionGroupEntity.getRuleId())) {
				rule = ruleService.getById(exhibitionList.get(0).getRuleId());
			} else {
				rule = ruleService.getById(positionGroupEntity.getRuleId());
			}

			RuleResponse ruleResponse = Objects.requireNonNull(BeanUtil.copy(rule, RuleResponse.class));
			ruleResponse.setRuleId(Func.toStr(rule.getId()));
			heartbeatResponse.setRuleResponse(ruleResponse);
		}
		//版本
		VersionEntity version = versionService.getOne(Wrappers.<VersionEntity>query().lambda()
			.eq(VersionEntity::getVenueId, heartbeatRequest.getVenueId())
			.orderByDesc(VersionEntity::getVersionRef).last("limit 1")
		);
		if (version != null) {
			heartbeatResponse.setVersionRef(version.getVersionRef());
			heartbeatResponse.setVersionUrl(version.getVersionUrl());
		}
		return heartbeatResponse;
	}

	@Override
	public PdaInittResponse padInit(String positionCode) {
		PositionEntity positionEntity = positionService.getOne(Wrappers.<PositionEntity>query().lambda().eq(PositionEntity::getPositionCode, positionCode));
		if (positionEntity == null) {
			throw new ServiceException("该门禁点不存在、请联系管理员进行创建！");
		}
		PdaInittResponse pdaInittResponse = BeanUtil.newInstance(PdaInittResponse.class);
		//查询展会
		List<ExhibitionEntity> exhibitionList = getListByPiontId(Func.toStr(positionEntity.getId()));

		List<ExhibitionResponse> exhibitionResponseList = new ArrayList<>();
		exhibitionList.forEach(o->{
			ExhibitionResponse exhibitionResponse = BeanUtil.newInstance(ExhibitionResponse.class);
			exhibitionResponse.setExhibitionId(o.getId());
			exhibitionResponse.setExhibitionName(o.getExhibitionAs());
			exhibitionResponseList.add(exhibitionResponse);
		});
		pdaInittResponse.setExhibitionResponses(exhibitionResponseList);
		//门禁点
		pdaInittResponse.setPositionId(Func.toStr(positionEntity.getId()));
		pdaInittResponse.setPositionName(positionEntity.getPositionName());
		//门禁组
		PositionGroupRelEntity positionGroupRel = positionGroupRelService.getOne(Wrappers.<PositionGroupRelEntity>query().lambda().eq(PositionGroupRelEntity::getPositionId, positionEntity.getId()));
		//基本信息
		PositionGroupEntity positionGroupEntity = positionGroupService.getById(positionGroupRel.getPositionGroupId());
		pdaInittResponse.setPositionGroupId(positionGroupEntity.getId());
		pdaInittResponse.setGroupName(positionGroupEntity.getGroupName());
		//策略
		if(Func.isNotEmpty(exhibitionList)){
			pdaInittResponse.setRuleId(Func.toStr(exhibitionList.get(0).getRuleId()));
		}
		return pdaInittResponse;

	}

	@NotNull
	private GateResponse getGateResponse(GateEntity gateEntity, PositionEntity positionEntity, GateResponse gateResponse) {
		VenueEntity venueEntity = venueService.getById(positionEntity.getVenueId());
		PositionGroupRelEntity positionGroupRel = positionGroupRelService.getOne(Wrappers.<PositionGroupRelEntity>query().lambda().eq(PositionGroupRelEntity::getPositionId, positionEntity.getId()));
		PositionGroupEntity positionGroupEntity = positionGroupService.getById(positionGroupRel.getPositionGroupId());
		return data(gateResponse, gateEntity, positionEntity, venueEntity, positionGroupEntity);
	}
}
